import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String s = in.next();
        int n = in.nextInt();
        String[] arr = new String[n];
        Arrays.sort(arr);
        for (int i = 0; i < n; i++) {
            arr[i] = in.next();
        }
        Map<Character, Integer> map = new HashMap<>();
        char[] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            map.put(c[i], i);
        }
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (compare(arr[j].toCharArray(), arr[j+1].toCharArray(), map) > 0) {
                    swap(arr, j, j+1);
                }
            }
        }
        for (String tmp : arr) {
            System.out.println(tmp);
        }
    }

    private static void swap(String[] arr, int x, int y) {
        String tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }

    private static int compare(char[] c1, char[] c2, Map<Character, Integer> map) {
        for (int i = 0; i < Math.max(c1.length, c2.length); i++) {
            if (i >= c1.length) {
                return -1;
            }
            if (i >= c2.length) {
                return 1;
            }
            if (map.get(c1[i]) - map.get(c2[i]) < 0) {
                return -1;
            } else if (map.get(c1[i]) - map.get(c2[i]) > 0) {
                return 1;
            }
        }
        return 0;
    }
}
